<!--
  - Copyright (c) 2018.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
  <div>
    <el-row>
      <el-col :span="4" align="left" style="line-height: 36px;height: 36px">
        <span style="font-size: 14px;color: #747778">家庭信息</span>
      </el-col>
    </el-row>
    <el-row style="justify-content: center">
      <el-button @click="addRow()"
                 style="margin: 8px 0px 8px 0px;padding: 6.2px 7px;font-size: 13px;line-height: 18px"
                 v-show="tableData.length === 0 && canEdit && editable" round>
        <i class="joys-icon-new" style="margin-right: 12px;margin-left: 4px"/>
        <span style="margin: 0px">添加家庭成员信息</span>
      </el-button>
      <el-button type="primary"
                 @click="addRow()"
                 style="padding: 0px 18px 0px 8px;line-height: 36px;margin: 12px 0px"
                 v-show="editable && canEdit && !showForm && tableData.length > 0" round>
        <i class="joys-icon-addagain" style="margin-right: 18px;"/>
        <span style="margin: 0px;display: inline-block;font-size: 14px;">继续添加</span>
      </el-button>
      <span v-if="tableData.length === 0 && !editable">没有数据</span>
    </el-row>

    <transition name="el-zoom-in-top">
      <el-form :model="formData" ref="data"
               :rules="rules"
               style="text-align: left;padding-top: 28px"
               :class="{cardBackColor:!canEdit}"
               v-show="showForm" inline>
        <el-row style="padding-left: 12px;padding-right: 12px;padding-bottom: 24px">
          <el-col :md="8" :lg="6">
            <el-form-item label="姓名" prop="memberName">
              <ft-input :editing="!canEdit" v-model="formData.memberName"
                        placeholder="请输入内容"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="证件类型" prop="memberIdentityType">
              <ft-enum-select v-model="formData.memberIdentityType"
                              service-name="lss-customer"
                              type-name="personalType"
                              placeholder="请选择"
                              :editing="!canEdit">
              </ft-enum-select>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="证件号码" prop="memberIdentityNo">
              <ft-input :editing="!canEdit" v-model="formData.memberIdentityNo"
                        placeholder="请输入内容"></ft-input>
            </el-form-item>
          </el-col>
          <el-col :md="8" :lg="6">
            <el-form-item label="家族关系" prop="relationType">
              <ft-data-enum-select
                v-model="formData.relationType"
                type-name="relationType"
                placeholder="请选家族关系"
                :editing="!canEdit">
              </ft-data-enum-select>
            </el-form-item>
          </el-col>
        </el-row>

        <hr style="background-color: #EDEDED;border: none;height: 1px;margin: 0px"/>

        <el-row style="padding:16px 0px">
          <el-col align="right">
            <el-button size="mini"
                       @click="handleCancel()"
                       v-show="!canEdit">取消
            </el-button>
            <el-button size="mini"
                       type="primary"
                       @click="handleSave()"
                       style="margin-left: 24px;margin-right: 36px"
                       v-show="!canEdit">保存
            </el-button>
          </el-col>
          <el-col align="right" v-show="canEdit">
            <el-button size="mini"
                       style="margin-left: 24px;margin-right: 36px"
                       @click="handleCancel()">返回
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </transition>
    <transition name="el-zoom-in-top">
      <el-table :data="tableData"
                tooltip-effect="light"
                v-show="tableData.length > 0 && !showForm">
        <el-table-column prop="memberName" label="姓名" align="left" show-overflow-tooltip>
          <template slot-scope="scope">
            <ft-link-text @click="handleCard(scope.$index,scope.row)"
                          :text="scope.row.memberName">
            </ft-link-text>
          </template>
        </el-table-column>
        <el-table-column prop="memberIdentityType" label="证件类型" align="left" show-overflow-tooltip>
          <template slot-scope="scope">
            <ft-enum-select v-model="scope.row.memberIdentityType"
                            service-name="lss-customer"
                            type-name="personalType"
                            placeholder="请选择"
                            :editing="tableEnumEdit">
            </ft-enum-select>
          </template>
        </el-table-column>
        <el-table-column prop="memberIdentityNo" label="证件号码" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column prop="relationType" label="家族关系" align="left">
          <template slot-scope="scope">
            <ft-data-enum-select
              v-model="scope.row.relationType"
              type-name="relationType"
              placeholder="请选家族关系"
              :editing="false">
            </ft-data-enum-select>
          </template>
        </el-table-column>
        <el-table-column v-if="editable" label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-popover title="确认" placement="top" width="200" v-model="scope.row.delVisible">
              <p>数据删除后，将不能恢复，确认删除？</p>
              <div style="text-align:right; margin:0">
                <el-button size="small"
                           @click="scope.row.delVisible=false">取消
                </el-button>
                <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">确定</el-button>
              </div>
              <span slot="reference" style="margin-left:.10rem">
                  <el-button type="text"
                             size="small"
                             style="color:#B94A48">删除</el-button>
                </span>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </transition>
  </div>
</template>

<script>
  import FtInput from "../ft/FtInput"

  import {identityFormat} from "@/pages/pc/assets/js/validate/rule.js"

  export default {
    name: "CustMember",
    components: {FtInput},
    props: {
      customerId: {
        type : String,
        default : '',
      },
      editable: {
        type : Boolean,
        default : true,
      },
    },
    mounted() {
      this.load()
    },
    data() {
      //根据证件号码判断是否合法
      let validateCustNum = (rule, value, callback) => {
        if (this.formData.memberIdentityType == '') {
          callback(new Error('请选择证件类型'))
        } else {
          if (this.formData.memberIdentityNo && value && value != '') {
            if (!identityFormat(this.formData.memberIdentityType, value)) {
              callback(new Error('输入证件号码不合法'))
            } else {
              callback()
            }
          } else {
            callback()
          }
        }
      };
      return {
        tableData: [],
        condition: [],
        tableEnumEdit: false,
        dataVersion: "",
        kmId: "",
        formBack: {},
        formData: {},
        showForm: {
          type: Boolean,
          default: false
        },
        canEdit: true,
        tableEnumEdit: false,
        rules: {
          memberName: [
            {required: true, message: '姓名不能为空', trigger: 'blur'},
            {max:30,message:'不得超过30字符'}],
          memberIdentityType: [
            {required: true,message: '证件类型不能为空',trigger: 'blur'}],
          memberIdentityNo: [
            {required: true,message: '证件号码不能为空',trigger: 'blur'},
            {max:20,message:'不得超过20字符'},
            {validator: validateCustNum, trigger: 'blur'}],
          relationType: [
            {required: true,message: '关系类型不能为空',trigger: 'blur'}],
        }
      }
    },
    methods: {
      data() {
        return {}
      },
      load() {
        this.$http.get('/joys-rest/lss-customer/member/'+this.customerId).then(
          resp => {
            this.tableData = resp.body;
            this.showForm = false
          },
          resp => {
            this.$message.error(resp.body)
        })
      },
      //列表编辑按钮
      handleEdit(val) {
        this.formData=JSON.parse(JSON.stringify(val));
        this.formBack =JSON.parse(JSON.stringify(val));
        this.canEdit = false;
        this.showForm = true
      },
      //列表删除按钮
      handleDelete(index, val) {
        this.memberId = val.custMemberId;
        this.$http.delete('/joys-rest/lss-customer/member/' + this.memberId).then(
          resp => {
            this.tableData.splice(index, 1);
            this.$message.success('删除成功!')
          },
          resp => {
            this.$message.error(resp.body)
        })
      },
      //列表取消
      handleCancel() {
        this.showForm = false;
        this.canEdit = true;
        this.formData=this.formBack
      },
      //列表增行
      addRow() {
        this.formData = {
          customerId: this.customerId,
          memberIdentityType: 'IDENTITY_CARD'
        };
        this.canEdit = false;
        this.showForm = true
      },
      //保存
      handleSave() {
        this.$refs.data.validate((valid) => {
          if (valid) {
            this.$http.put('/joys-rest/lss-customer/member', this.formData, {
              params: {
                sourceModule: '家庭信息',
              }
            }).then(
              resp => {
                this.canEdit = true;
                this.showForm = false;
                this.load();
                this.$message.success('保存成功!')
              },
              resp => {
                this.$message.error(resp.body);
                this.canEdit = false
            })
          }
        })
      },
      //列表卡片按钮
      handleCard(index, row) {
        this.formData = row;
        this.canEdit = true;
        this.showForm = true
      },
    },
  }
</script>
<style>

</style>
<style scoped>
</style>
